package com.emar.adx.yiqifa.reflect;

import com.emar.adx.yiqifa.entity.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaoliang on 2015/11/6 13:20.
 *
 * 简单反射原理测试。
 *
 */
public class SimpleReflect {

    private Logger log = LoggerFactory.getLogger(SimpleReflect.class);

    /**
     * 获取类名，包名
     * @param user
     */
    public List<String> getClassName(User user){
        String className = user.getClass().getName();
        Package aPackage = user.getClass().getPackage();
        aPackage.getName();

        List<String> list = new ArrayList<String>();
        log.debug("getClassName : "+className);
        log.debug("getPackage getName : "+aPackage.getName());

        list.add(className);
        list.add(aPackage.getName());

        return list;
    }



    public void classLoad() throws Exception{
        URL url  =   new URL("file:///D:\\WorkSpace\\idea14\\bee\\bee-spider\\target\\classes\\com\\emar\\adx\\yiqifa\\bee\\spider\\analysis\\requester\\test.jar");

        URLClassLoader myClassLoader  =   new  URLClassLoader( new  URL[]  { url } , Thread.currentThread().getContextClassLoader());
        Class test = myClassLoader.loadClass("com.emar.adx.yiqifa.bee.entity.Node");

        Object obj = test.newInstance();
    }


    public void classLoad2() throws Exception{
        ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader();
        Class test = myClassLoader.loadClass("com.emar.adx.yiqifa.bee.entity.Node");

        Object obj = test.newInstance();
    }

    public static void main(String[] args) {
        SimpleReflect simpleReflect = new SimpleReflect();
        try {
            simpleReflect.classLoad();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
